package com.xu.rag.factory;

import com.xu.rag.common.annotation.ModelType;
import com.xu.rag.service.LlmService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: xdl
 * @CreateTime: 2025-01-15 23:18
 */
@Component
public class ModelFactory {
    private static Map<String, LlmService> map = new ConcurrentHashMap<>();
    public static LlmService getModel(String modelCode) {
        if (StringUtils.isBlank(modelCode)) {
            throw new RuntimeException("modelCode must not null!");
        }
        LlmService llmService = map.get(modelCode);
        if (llmService == null) {
            throw new RuntimeException("no model find!");
        }
        return llmService;
    }
    public ModelFactory(LlmService[] llmServices) {
        for (LlmService llmService : llmServices) {
            String modelCode = AnnotationUtils.findAnnotation(llmService.getClass(), ModelType.class).modelCode();
            map.put(modelCode, llmService);
        }
    }
}
